﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using VSDBM.Code.dto;
using System.Data;
using MySql.Data.MySqlClient;

namespace VSDBM.Code.dao
{
    public class Subtype_ReferenceDAO
    {
        public static String add(Subtype_Reference refsub)
        {
            try
            {
                    MySqlConnection connection = Connection.getConnection();
                    MySqlCommand query = new MySqlCommand("INSERT INTO subtypes_references_sequences (idsequence,idsubtype) VALUES (" + refsub.idSequence + "," + refsub.idSubtype + ")", connection);
                    query.ExecuteNonQuery();
                    return "Added!";
                
            }
            catch (Exception e)
            {
                return "ERROR! - " + e.Message;
            }
        }

        public static Boolean delete(int id)
        {
            try
            {
                MySqlConnection connection = Connection.getConnection();
                MySqlCommand query = new MySqlCommand("DELETE FROM subtypes_references_sequences WHERE id = " + id + "", connection);
                query.ExecuteNonQuery();
                return true;
            }
            catch (Exception e)
            {
                throw e ;
            }
        }

      

        public static DataSet select(String filters, String orderby)
        {

            DataSet dSet = new DataSet();
            try
            {
                MySqlConnection connection = Connection.getConnection();
                MySqlDataAdapter adapter = new MySqlDataAdapter();
                adapter.SelectCommand = new MySqlCommand("SELECT * FROM subtypes_references_sequences " + filters + " " + orderby, connection);
                adapter.Fill(dSet);
                return dSet;
            }
            catch (Exception e)
            {
                return dSet;
            }
        }

        public static Subtype_Reference Fill(int id, int idSequence, int idSubtype)
        {
            Subtype_Reference refsub = new Subtype_Reference();
            
            return refsub;
        }

        public static List<Subtype_Reference> DStoList(DataSet ds)
        {
            List<Subtype_Reference> list = new List<Subtype_Reference>();
            try
            {
                for (int i = 0; i <= ds.Tables[0].Rows.Count - 1; i++)
                {
                    DataTable registro = ds.Tables[0];
                    list.Add(Fill(int.Parse(registro.Rows[i]["id"].ToString()), int.Parse(registro.Rows[i]["idsequence"].ToString()), int.Parse(registro.Rows[i]["idsubtype"].ToString())));
                }

            }
            catch (Exception e) { }
            return list;
        }
    }
}